package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class ImagesActivity extends Activity implements View.OnClickListener {

    ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
    private int[] images = new int[]{
            R.drawable.gallery_picture01, R.drawable.gallery_picture02,
            R.drawable.gallery_picture03, R.drawable.gallery_picture04,
            R.drawable.gallery_picture05, R.drawable.gallery_picture06,
            R.drawable.gallery_picture07
    };              //图像数组
    private int index = 0;             //下标

    private ImageSwitcher is;         //切换器

    private Button up, down;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_images);

        is = (ImageSwitcher) findViewById(R.id.images_imageSwitcher1);

        up = (Button) findViewById(R.id.images_up);
        down = (Button) findViewById(R.id.images_down);
        up.setOnClickListener(this);
        down.setOnClickListener(this);

        initImages();


        is.setFactory(new ViewSwitcher.ViewFactory() {

            @Override
            public View makeView() {
                ImageView imageView = new ImageView(ImagesActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

//                android:adjustViewBounds="true"
//                android:maxWidth="360dp"
//                android:maxHeight="320dp"
                int dp = UnitOfMeasure.dip2px(ImagesActivity.this, 300);
                imageView.setAdjustViewBounds(true);
                imageView.setMaxHeight(dp);


                return imageView;
            }
        });
        is.setImageResource(images[index]);

    }

    @Override
    public void onClick(View v) {
        Log.e("v.getID() is ", String.valueOf(v.getId()));
        switch (v.getId()) {

            case R.id.images_up:

                if (index > 0) {
                    index--;
                } else {
                    index = images.length - 1;
                }
                is.setImageResource(images[index]);
                return;
            case R.id.images_down:
                if (index == images.length - 1) {
                    index = 0;
                } else {
                    index++;
                }
                is.setImageResource(images[index]);
                return;
            default:
                ;
                break;
        }
        for (int i = 0; i < images.length; i++) {
            if (v.getId() == imageViews.get(i).getId()) {
                is.setImageResource(images[i]);
            } else continue;
        }

    }


    private void initImages() {
        imageViews.add((ImageView) findViewById(R.id.images_picture01));
        imageViews.add((ImageView) findViewById(R.id.images_picture02));
        imageViews.add((ImageView) findViewById(R.id.images_picture03));
        imageViews.add((ImageView) findViewById(R.id.images_picture04));
        imageViews.add((ImageView) findViewById(R.id.images_picture05));
        imageViews.add((ImageView) findViewById(R.id.images_picture06));
        imageViews.add((ImageView) findViewById(R.id.images_picture07));

        for (ImageView item : imageViews) {
            item.setOnClickListener(this);
        }
    }
}
